import time
import random
import threading
from connection import PhoenixConnect

conn: PhoenixConnect
avid = 0

imgs = ['http://xiaoma02/images/1.webp',
        'http://xiaoma02/images/2.webp',
        'http://xiaoma02/images/3.webp',
        'http://xiaoma02/images/4.webp',
        'http://xiaoma02/images/5.webp',
        'http://xiaoma02/images/6.webp',
        'http://xiaoma02/images/7.webp',
        'http://xiaoma02/images/8.webp',
        'http://xiaoma02/images/9.webp',
        'http://xiaoma02/images/10.webp']

cities = ['Beijing', 'Shanghai', 'Guangzhou', 'shenzhen', 'Los Angeles', 'New York', 'Chicago', 'Las Vegas']

coords = [[116, 39], [121, 31], [113, 23], [114, 23], [-118, 34], [-73, 40], [-87, 41], [-115, 36]]

done = 0


def current_milli():
    return int(round(time.time() * 1000))


class DataCreator:
    conn = None
    cursor = None
    tid: int
    start: int
    end: int

    def __init__(self, url, tid, start, end):
        self.conn = PhoenixConnect(url)
        self.cursor = self.conn.get_update_cursor()
        self.tid = tid
        self.start = start
        self.end = end

    def exec_updates(self, sqls):
        try:
            for sql in sqls:
                # print(sql)
                self.cursor.execute(sql)
        except Exception as ex:
            print(ex)

    def select(self, sql):
        return self.conn.select(sql)

    # 为内容系统创建完整数据
    def create_data(self):
        global done
        print('Thread %d started...' % self.tid)

        try:
            # account
            for i in range(self.start, self.end + 1):
                sqls = []
                aid = i
                name = 'acc_%d' % i
                disp_name = 'name_%d' % i
                uid = random.randint(1, 1000)

                if random.randint(0, 1) == 0:
                    gender_name = 'M'
                else:
                    gender_name = 'F'

                sql = "UPSERT INTO C_COMMON.ACCOUNT(" \
                      "ID, SSID, WALLET_AID, NAME, DISP_NAME," \
                      " GENDER, IMAGE_URL, STATUS, VERIFIED, FIRST_TIME," \
                      " CREATE_UID, CREATE_DT)" \
                      " VALUES(%d, 'welog-tx', '%d', '%s', '%s'," \
                      " '%s', '%s', '', false, true," \
                      " '%d', 0)" % (aid, aid, name, disp_name, gender_name, imgs[(i - 1) % 10], uid)

                sqls.append(sql)

                # 创建账号-AV关系
                self.gen_av_upserts(aid, gender_name, sqls)
                self.exec_updates(sqls)
        except Exception as ex:
            print('%d号线程异常：' % self.tid)
            print(ex)

        done = done + 1

    def gen_av_upserts(self, aid, gender, sqls):
        global avid

        city_count = len(cities) - 1

        for i in range(1, 11):
            avid = avid + 1
            city = cities[random.randint(0, city_count)]
            sql = "UPSERT INTO WELOG_SV.AV(ID, AID, A_GENDER, TITLE, CITY)" \
                  " VALUES(%d, %d, '%s', 'AV_%d', '%s')" % (avid, aid, gender, avid, city)
            sqls.append(sql)


if __name__ == '__main__':
    # 创建 4000 个账号
    url = 'http://mng1:8765/'

    dc1 = DataCreator(url, 1, 1, 1000)
    dc2 = DataCreator(url, 2, 1001, 2000)
    dc3 = DataCreator(url, 3, 2001, 3000)
    dc4 = DataCreator(url, 4, 3001, 4000)

    t1 = threading.Thread(target=dc1.create_data, args=())
    t2 = threading.Thread(target=dc2.create_data, args=())
    t3 = threading.Thread(target=dc3.create_data, args=())
    t4 = threading.Thread(target=dc4.create_data, args=())

    t1.start()
    t2.start()
    t3.start()
    t4.start()

    while done < 4:
        print("AV_ID: %d" % avid)
        time.sleep(3)
